/**
 * Created by 王健 on 2018/2/21.
 */

var map;
var geocoder;

function codeAddress(address) {
    var defer = $.Deferred();
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == 'OK') {
            map.setCenter(results[0].geometry.location);
            // var marker = new google.maps.Marker({
            //     map: map,
            //     position: results[0].geometry.location
            // });
            map.setZoom(10);
            defer.resolve();
        } else {
            defer.reject();
            alert('Geocode was not successful for the following reason: ' + status);
        }
    });

    return defer.promise();
}

function locationAddress(location) {
    var defer = $.Deferred();
    geocoder.geocode( { 'location': location}, function(results, status) {
        if (status == 'OK') {
            var place = "";
            try{
                _(results[0].address_components).each(function (item) {
                    _(item.types).each(function (obj) {
                        if(obj=='country'){
                            place += item.long_name;
                        }
                    });
                });
                _(results[0].address_components).each(function (item) {
                    _(item.types).each(function (obj) {
                        if(obj=='locality'){
                            place += item.long_name;
                        }
                    });
                });
                _(results[0].address_components).each(function (item) {
                    _(item.types).each(function (obj) {
                        if(obj=='sublocality'){
                            place += item.long_name;
                        }
                    });
                });
            }catch (error){

            }

            defer.resolve(place);
        } else {
            defer.reject();
            alert('Geocode was not successful for the following reason: ' + status);
        }
    });

    return defer.promise();
}


function b_init(lat, lng, zoom) {
    var defer = $.Deferred();
    // google地图API功能
    if(!geocoder){
        try {
            geocoder = new google.maps.Geocoder();
        }catch (e) {

        }
    }
    if(!lat){
        lat = 24.271025738399786;
    }
    if(!lng){
        lng = 108.17826741000363;
    }
    if(!zoom){
        zoom=6;
    }
    setTimeout(function () {
        try{
            map = new google.maps.Map(document.getElementById('allmap'), {
                center: {lat: lat, lng: lng},
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                zoom: zoom
            });
        }catch (e) {

        }

        defer.resolve();
    }, 300);


    return defer.promise();
}

function b_current_position() {
    var defer = $.Deferred();
    var geolocation = new BMap.Geolocation();
    geolocation.getCurrentPosition(function(r){
        if(this.getStatus() == BMAP_STATUS_SUCCESS){
            // var mk = new BMap.Marker(r.point);
            // map.addOverlay(mk);
            // map.panTo(r.point);
            // alert('您的位置：'+r.point.lng+','+r.point.lat);
            defer.resolve(r.point);

        }
        else {
            // alert('failed'+this.getStatus());
        }
    },{enableHighAccuracy: true})

    return defer.promise();
}

function GetDistance( lat1,  lng1,  lat2,  lng2){
    var radLat1 = lat1*Math.PI / 180.0;
    var radLat2 = lat2*Math.PI / 180.0;
    var a = radLat1 - radLat2;
    var  b = lng1*Math.PI / 180.0 - lng2*Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
        Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
    s = s *6378.137 ;// EARTH_RADIUS;
    s = Math.round(s * 10000) / 10000;
    return s;
}